﻿using IconYuan.Manager.DB;
using IconYuan.Model.DB;
using IconYuan.Model.Products;
using SqlSugar;
using System.Linq.Expressions;

namespace IconYuan.Manager.Products
{
    /// <summary>
    /// 商品
    /// </summary>
    public class GoodsManager : BaseContext<Goods>
    {
        public T GetBase<T>(object id, bool blUseNoLock = false)
        {
            return blUseNoLock
                ? Db.Queryable<T>().With(SqlWith.NoLock).InSingle(id)
                : Db.Queryable<T>().InSingle(id);
        }

        public PageList<GoodsBase> GetPages(Expression<Func<GoodsBase, bool>> where, Expression<Func<GoodsBase, object>> orderByExpression,
            OrderByType orderByType, int pageIndex = 1, int pageSize = 20)
        {
            int totalCount = 0;
            var list = Db.Queryable<GoodsBase>()
                .WhereIF(where != null, where)
                .OrderBy(x => x.sort, orderByType)
                .ToPageList(pageIndex, pageSize, ref totalCount);

            return ToPageList(list, pageIndex, pageSize, totalCount);
        }

        public PageList<GoodsBase> GetPages(int shop_id, List<int> category_ids, int page, int pageSize)
        {
            var count = 0;
            var exp = Expressionable.Create<GoodsBase>()
                .And(x => x.status == 10)
                .AndIF(shop_id > 0, x => x.shop_id == shop_id)
                .AndIF(category_ids.Count() > 0, x => category_ids.Contains(x.category_id)).ToExpression();
            var list = Db.Queryable<GoodsBase>()
                .Where(exp)
                .OrderBy(x => x.sort, OrderByType.Asc)
                .ToPageList(page, pageSize, ref count);

            return ToPageList(list, page, pageSize, count);
        }

        /// <summary>
        /// 所有商品
        /// </summary>
        /// <param name="shop_id"></param>
        /// <param name="category_ids"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<GoodsBase>> GetAllAsync(int shop_id, List<int> category_ids)
        {
            var exp = Expressionable.Create<GoodsBase>()
                .And(x => x.status == 10)
                .AndIF(shop_id > 0, x => x.shop_id == shop_id)
                .AndIF(category_ids.Count() > 0, x => category_ids.Contains(x.category_id));

            //foreach(var id in category_ids)
            //{
            //    exp.And()
            //}
            var list = await Db.Queryable<GoodsBase>()
                .Where(exp.ToExpression())
                .OrderBy(x => x.sort, OrderByType.Asc)
                .ToListAsync();
            return list;
        }

        /// <summary>
        /// 更新库存和实际销量
        /// </summary>
        /// <param name="goods_id"></param>
        /// <param name="buy_count"></param>
        /// <returns></returns>
        public bool UpdateStock(int goods_id, int buy_count)
        {
            return Update(x => new Goods() { stock_total = x.stock_total - buy_count, sales_actual = x.sales_actual + buy_count }, y => y.goods_id == goods_id);
        }
    }
}
